<?php
	define('DS', DIRECTORY_SEPARATOR);
	defined('HTML_PATH') or define('HTML_PATH', dirname(realpath(dirname(realpath($_SERVER['SCRIPT_FILENAME'])))) . DS);
    function fetch_dir($dir, $file_type = null) {
        $base_dir = realpath($dir);
        if (!file_exists($base_dir)) {
            return false;
        }
        $dir_handle = opendir($base_dir);
        $files_list = array();
        while (($file = readdir($dir_handle)) !== false) {
            if (substr($file, 0, 1) != '.' AND is_dir($base_dir . DS . $file)) {
                $files_list[] = $base_dir . DS . $file;
            }
        }
        closedir($dir_handle);
        return $files_list;
    }
    
    
    function fetch_file_lists($dir, $file_type = null) {
       if ($file_type) {
    	    if (substr($file_type, 0, 1) == '.') {
                $file_type = substr($file_type, 1);
            }
        }
    
        $base_dir = realpath($dir);
    
        if (!file_exists($base_dir)) {
            return false;
        }
    
        $dir_handle = opendir($base_dir);
    
        $files_list = array();
    
        while (($file = readdir($dir_handle)) !== false) {
            if (substr($file, 0, 1) != '.' AND !is_dir($base_dir . DS . $file)) {
                if (($file_type AND end(explode('.', $file)) == $file_type) OR !$file_type) {
                    $files_list[] = $base_dir . DS . $file;
                }
            }
            else if (substr($file, 0, 1) != '.' AND is_dir($base_dir . DS . $file)) {
                if ($sub_dir_lists = fetch_file_lists($base_dir . DS . $file, $file_type)) {
                    $files_list = array_merge($files_list, $sub_dir_lists);
                }
            }
        }
        closedir($dir_handle);
        return $files_list;
    }
    
    $tempComponent = '';
	$pages = [];
	$pageTemplatePath = HTML_PATH . 'html' . DS;
    $cssList = fetch_dir($pageTemplatePath);
    foreach ($cssList as $key => $file) {
        $tempArray = explode(DS, $file);
        $lastFileName = $tempArray[count($tempArray) - 1];
        if (preg_match('/^[a-zA-Z\s]+$/',$lastFileName)) {
            $cssFilePath = $file . DS . 'vue' . DS;
            $cssFilePathArray = fetch_file_lists($cssFilePath);
            if ($cssFilePathArray) {
                foreach ($cssFilePathArray as $k => $v) {
                    if (strpos($v, '.vue') !== false) {
                        $cssArray = explode(DS, $v);
                        array_push($pages,['pagePath' => $v,'pageName' => $cssArray[count($cssArray) - 1]]);
                    }
                }
            }
        }
    }     
    if ($pages) {
        foreach ($pages as $key => $val) {
            $fileName = str_replace('.vue', '', $val['pageName']);
            $tempIndexData = file_get_contents($val['pagePath']);
            preg_match_all('/<script>([\w\W]*)<\/script>/iU', $tempIndexData, $scriptResult);
            if ($scriptResult) {
                preg_match_all('/<style>([\w\W]*)<\/style>/iU', $tempIndexData, $templateResult);
                $tempComponent .=  $templateResult[1][0];
            } 
        }
    }
    $tempComponent = preg_replace( '/ {2,}/', '',$tempComponent);
    
    header('Content-Type: text/css; charset=UTF-8');
    echo $tempComponent;
